
    $(function () {
        var flagAccount = true;
        var accountForm;

        $("#apartmentname").change(function () {
            accountForm = $("#apartmentname").val();
            if (accountForm == "") {
                $("#apartmentname").removeClass("form-control is-valid")
                $("#apartmentname").addClass("form-control is-invalid");
                flagAccount = false;
            } else {
                $("#apartmentname").removeClass("form-control is-invalid")
                $("#apartmentname").addClass("form-control is-valid");
                flagAccount = true;
            }
        })
        $("#register").click(function () {
            if (flagAccount) {
                return true;
            } else {
                if (!flagAccount) {
                    $("#usernameInfo").html("未输入部门名称或已存在");
                    $("#usernameInfo").css("font-size", "80%");
                    $("#usernameInfo").css("color", "#dc3545");
                    $("#apartmentname").addClass("form-control is-invalid");
                }
                return false;
            }
        })
    })


$(function () {
    //1.为输入框绑定事件,失去焦点获得输入框内容
    $("#apartmentname").blur(function () {
        var usernameInput = $(this).val();
        var apartid = $("#apartmentid").val();
        console.log(apartid);
        // var data = "apartmentname=" + usernameInput + "&apartmentid=" + apartid;
        //2.去服务器端校验用户名是否存在 ---ajax
        $.ajax({
            url: "/admin/findUpdateapart",
            type: "POST",
            dataType: "json",
            data:{"apartmentname": usernameInput,"apartmentid": apartid},
            success: function (data) {
                var isExist = data.isExist;
                //根据返回的isExist动态显示信息
                var usernameInfo = "";
                $("#usernameInfo").css("font-size", "80%");
                if (isExist || ($("#apartmentname").val() == "")) {
                    //用户存在
                    usernameInfo = "未输入部门名称或已存在"
                    $("#usernameInfo").css("color", "#dc3545");
                    $("#apartmentname").addClass("is-invalid");

                } else {
                    usernameInfo = "该部门名称可以使用"
                    $("#usernameInfo").css("color", "green");
                    $("#apartmentname").addClass("is-valid");
                }
                $("#usernameInfo").html(usernameInfo);
            }
        });
    });
})